<template>
  <div>
    <div class="table-toolbar" v-if="hasToolbar">
      <div class="table-toolbar-left">
        <slot name="toolbarBtn"></slot>
      </div>
      <div class="table-toolbar-right flex-center">
        <template v-if="multipleSelection.length > 0">
          <el-tooltip effect="dark" content="删除选中" placement="top">
            <el-icon
              class="columns-setting-icon"
              @click="delSelection(multipleSelection)"
            >
              <Delete />
            </el-icon>
          </el-tooltip>
          <el-divider direction="vertical" />
        </template>
        <el-tooltip effect="dark" content="刷新" placement="top">
          <el-icon class="columns-setting-icon" @click="$emit('refresh')">
            <Refresh />
          </el-icon>
        </el-tooltip>
        <el-divider direction="vertical" />
        <el-tooltip effect="dark" content="列设置" placement="top">
          <el-dropdown :hide-on-click="false" size="small" trigger="click">
            <el-icon class="columns-setting-icon">
              <Setting />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item v-for="c in columns">
                  <el-checkbox v-model="c.visible" :label="c.label" />
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-tooltip>
      </div>
    </div>
    <el-table
      class="mgb20"
      :style="{ width: '100%' }"
      border
      :data="tableData"
      :highlight-current-row="props.highlightCurrentRow"
      :row-key="rowKey"
      @selection-change="handleSelectionChange"
      @row-click="rowClick"
      table-layout="auto"
    >
      <template v-for="item in columns" :key="item.prop">
        <el-table-column
          v-if="item.visible"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :type="item.type"
          :align="item.align || 'center'"
        >
          <template
            #default="{ row, column, $index }"
            v-if="item.type === 'index'"
          >
            {{ getIndex($index) }}
          </template>
          <template v-if="item.slot" #default="scope">
            <slot
              :name="item.slot"
              :row="scope.row"
              :record="scope.row"
              :index="scope.$index"
            >
            </slot>
          </template>
          <template v-slot:default="{ row, column, $index }" v-if="!item.type">
            <slot :name="item.prop" :rows="row" :index="$index">
              <template v-if="item.prop == 'operator'">
                <el-button
                  type="warning"
                  size="small"
                  v-if="item.isRun"
                  :icon="View"
                  @click="viewFunc(row)"
                >
                  运行
                </el-button>
                <el-button
                  type="primary"
                  size="small"
                  :icon="Edit"
                  @click="editFunc(row)"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  size="small"
                  :icon="Delete"
                  @click="handleDelete(row)"
                >
                  删除
                </el-button>
              </template>
              <span v-else-if="item.formatter">
                {{ item.formatter(row[item.prop]) }}
              </span>
              <!-- <span v-else-if="item.scopedSlots">
                123
                <slot
                  name="dynamic"
                  :row="row"
                  :column="column"
                  :record="row"
                  :index="$index"
                ></slot>
              </span> -->
              <span v-else>
                <!-- {{ row }} -->
                {{ row[item.prop] }}
              </span>
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <el-pagination
      v-if="hasPagination"
      :current-page="currentPage"
      :page-size="pageSize"
      :background="true"
      :layout="layout"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { toRefs, PropType, ref, inject } from "vue";
import { Delete, Edit, View, Refresh } from "@element-plus/icons-vue";
import { ElMessageBox } from "element-plus";

const props = defineProps({
  // 表格相关
  tableData: {
    type: Array,
    default: [],
  },
  columns: {
    type: Array as PropType<any[]>,
    default: [],
  },
  rowKey: {
    type: String,
    default: "id",
  },
  hasToolbar: {
    type: Boolean,
    default: true,
  },
  //  分页相关
  hasPagination: {
    type: Boolean,
    default: true,
  },
  total: {
    type: Number,
    default: 0,
  },
  currentPage: {
    type: Number,
    default: 1,
  },
  pageSize: {
    type: Number,
    default: 10,
  },

  layout: {
    type: String,
    default: "total, prev, pager, next",
  },
  delFunc: {
    type: Function,
    default: () => {},
  },
  viewFunc: {
    type: Function,
    default: () => {},
  },
  editFunc: {
    type: Function,
    default: () => {},
  },
  delSelection: {
    type: Function,
    default: () => {},
  },
  refresh: {
    type: Function,
    default: () => {},
  },
  changePage: {
    type: Function,
    default: () => {},
  },
  highlightCurrentRow: {
    type: Boolean,
    default: false,
  },
});

let {
  tableData,
  columns,
  rowKey,
  hasToolbar,
  hasPagination,
  total,
  currentPage,
  pageSize,
  layout,
} = toRefs(props);

columns.value.forEach((item) => {
  if (item.visible === undefined) {
    item.visible = true;
  }
});

const emit = defineEmits<{
  (e: "update:modelValue", value: string): void;
  (e: "changePage", event: any): void;
  (e: "refresh"): void;
  (e: "row-click", value: string): void;
}>();

// 当选择项发生变化时会触发该事件
const multipleSelection = ref([]);
const handleSelectionChange = (selection: any[]) => {
  multipleSelection.value = selection;
};
// 默认的空函数
const sendtemplate = inject<(childId: string, childName: string) => void>(
  "sendtemplate",
  () => {}
);
const sendnaturalRes = inject<(resId: string, resName: string) => void>(
  "sendnaturalRes",
  () => {}
);
//当前行点击
const rowClick = (row) => {
  console.log("row", row);
  sendtemplate(row.documentId, row.documentName);
  sendnaturalRes(row.resId, row.resName);
  // 注入爷组件提供的方法
};

// 当前页码变化的事件
const handleCurrentChange = (val: number) => {
  console.log("当前页码变化的事件", val);
  emit("changePage", val);

  // props.changePage(val)
};

const handleDelete = (row) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
  })
    .then(async () => {
      props.delFunc(row);
    })
    .catch(() => {});
};

const getIndex = (index: number) => {
  return index + 1 + (currentPage.value - 1) * pageSize.value;
};
</script>

<style scoped>
.table-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
}

.columns-setting-icon {
  display: block;
  font-size: 18px;
  cursor: pointer;
  color: #676767;
}
</style>
<style>
.table-header .cell {
  color: #333;
}
</style>
